<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 
 * Run Controller
 * @author Denis, Dennis, Simon
 *
 */
class Run extends CI_Controller {


	public function __construct()
	{
		parent::__construct();
		require_once('user.php');
		$this->user=new User();
	}

	/**
	* Index Page for this controller.
	*
	*/
	public function index()
	{

		$this->load->view('header');
		$loggedIn = $this->user->logged_in();
		$loginData['isLoggedIn'] = $loggedIn;
		if($loggedIn){
			$loginData['username'] = $_SESSION['username'];
		}
		$this->load->view('login', $loginData);

		$menu['activeController'] = 'run';
		$this->load->view('menu',$menu);
		if($loggedIn==true) {
			$this->load->view('run');
		} else {
			$this->load->view('503');
		}
		$this->load->view('footer');
	}
/**
 * method to add a new run with the data from the run view
 */
	public function newRun (){

		$this->load->view('header');
		$loggedIn = $this->user->logged_in();
		$loginData['isLoggedIn'] = $loggedIn;
		if($loggedIn){
			$loginData['username'] = $_SESSION['username'];
		}
		$this->load->view('login', $loginData);

		$menu['activeController'] = 'run';
		$this->load->view('menu',$menu);

		$data['routes'] = $this->routes_model->getRouteFromUser($_SESSION['id']);

		$userShoes = $this->shoeUser_model->getAllUserShoesFromUser($_SESSION['id']);
		$shoeNames = array();
		if(count($userShoes)>0) {
			foreach($userShoes as $userShoe){
				$shoe = $this->shoe_model->getShoe($userShoe->ShoeID);
				$shoe->Favorite = 0;
				$shoeNames[] = $shoe;
			}
		}
		$data['equipment'] = $shoeNames;
		$this->load->view('run_newrun', $data);

		$this->load->view('footer');
	}

/**
 * method to edit a run. uses the data from the run view
 */
	public function editRun (){
		$this->load->view('header');
		$loggedIn = $this->user->logged_in();
		$loginData['isLoggedIn'] = $loggedIn;
		if($loggedIn){
			$loginData['username'] = $_SESSION['username'];
		}
		$this->load->view('login', $loginData);

		$menu['activeController'] = 'run';
		$this->load->view('menu',$menu);



		$allRuns = $this->run_model->getRunsFromUser($_SESSION['id']);
		if(count($allRuns)>0) {
			foreach ($allRuns as $i => $run){

				$route = $this->routes_model->getRouteByID($run->Route);
				$run->route=$route;
			}
		}
		$data['runs'] = $allRuns;
		$data['routes'] = $this->routes_model->getRouteFromUser($_SESSION['id']);
		$equipment = $this->shoeUser_model->getAllUserShoesFromUser($_SESSION['id']);
		foreach ($equipment as &$shoeUser){
			$shoeObj = $this->shoe_model->getShoe($shoeUser->ShoeID);
			$shoeUser->Modell = $shoeObj->Modell;
			$shoeUser->Manufactor = $shoeObj->Manufactor;
			$shoeUser->RunEffort = $shoeObj->RunEffort;
// 			$shoeUser->Favorite = $shoeObj->Favorite;
			
		}
		 
		 $data['equipment'] = $equipment;

		$this->load->view('run_edit', $data);
		$this->load->view('footer');

	}

/**
 * method to update a user run with the given data from the run view
 */
	public function updateRun (){
		$runID = trim($_POST['runID']);
		$routeID = trim($_POST['routeID']);
		$avgPulse = trim($_POST['avgPulse']);
		$shoe = trim($_POST['shoeID']);
		$calorieConsumption = trim($_POST['calorieConsumption']);
		$duration = trim($_POST['duration']);
		$comment = trim($_POST['comment']);
		
		$date = trim($_POST['date']);
		$time = trim($_POST['time']);
		
		$dateParts = explode(".", $date);
		$timeParts = explode(":", $time);
		
		//hour,minute,second,month,day,year to timestamp
		$timestamp=mktime($timeParts[0], $timeParts[1], 0,$dateParts[0], $dateParts[1], $dateParts[2]);
		$datetime = date("Y-m-d H:i:s", $timestamp);
		
		$distance = $this->routes_model->getRouteByID($routeID)->Length;
		$avgSpeed = $distance/$duration;
		
		$error = $this->run_model->updateRun($runID, $routeID, $datetime, $avgSpeed, $avgPulse, $shoe, $calorieConsumption, $duration, $comment );
		if($error != 1){
			echo $error;
			return;
		}


		$this->load->view('header');
		$loggedIn = $this->user->logged_in();
		$loginData['isLoggedIn'] = $loggedIn;
		if($loggedIn){
			$loginData['username'] = $_SESSION['username'];
		}
		$this->load->view('login', $loginData);

		$menu['activeController'] = 'run';
		$this->load->view('menu',$menu);

		$this->load->view('run');
		$this->load->view('footer');

	}
	
	/**
	* method to add a new run to the DB with the data from the run view
	*/
	public function addRun (){
		$routeID = trim($_POST['routeID']);
		$avgPulse = trim($_POST['avgPulse']);
		$shoeID = trim($_POST['shoeID']);
		$calorieConsumption = trim($_POST['calorieConsumption']);
		$duration = trim($_POST['duration']);
		$comment = trim($_POST['comment']);
		
		$date = trim($_POST['date']);
		$time = trim($_POST['time']);
		
		$dateParts = explode(".", $date);
		$timeParts = explode(":", $time);
		//mktime = hour,minute,second,month,day,year to timestamp
		$timestamp=mktime($timeParts[0], $timeParts[1], 0,$dateParts[0], $dateParts[1], $dateParts[2]);
		$datetime = date("Y-m-d H:i:s", $timestamp);
		
		
		$distance = intval($this->routes_model->getRouteByID($routeID)->Length);
		$avgSpeed = $distance/$duration;
		
		$currentScalepoints=intval($this->user_model->getUserByID($_SESSION['id'])->Scalepoints);
		$newScalepoints=$currentScalepoints+$distance;
		
		$error = $this->run_model->addRun($_SESSION['id'], $routeID, $datetime, $avgSpeed, $avgPulse, $shoeID, $calorieConsumption, $duration, $comment );
		if($error!=1){
			echo $error;
			return;
		}
		
		$error = $this->user_model->updateScalepoints($_SESSION['id'],$newScalepoints);
		if($error!=1){
			echo $error;
			return;
		}
		
		
		$this->load->view('header');
		$loggedIn = $this->user->logged_in();
		$loginData['isLoggedIn'] = $loggedIn;
		if($loggedIn){
			$loginData['username'] = $_SESSION['username'];
		}
		$this->load->view('login', $loginData);

		$menu['activeController'] = 'run';
		$this->load->view('menu',$menu);
		$this->load->view('run');
		$this->load->view('footer');

	}


}